package ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.peng.auctionsystem.R;

import Widget.CustomToolBar;
import base.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import entity.BiddingEntity;
import manager.ApiConstants;
import manager.ApiRequestManager;
import manager.RxSchedulers;
import manager.RxSubscriber;
import utils.Constants;
import utils.ToastUtil;

/**
 * 竞拍详情界面
 */
public class ProductDetailActivity extends BaseActivity implements CustomToolBar.OnToolBarClickListener {

    private static final String TAG = "ProductDetailActivity";

    private int userId;
    private int commodityId;

    @Bind(R.id.detail_tv_price)
    TextView detailTvPrice;// 现在的价格
    @Bind(R.id.detail_tv_join)
    TextView detailTvJoin;// 竞价
    @Bind(R.id.custom_toolbar)
    CustomToolBar customToolbar;
    @Bind(R.id.detail_iv_pic)
    ImageView detailIvPic;// 竞品图片
    @Bind(R.id.detail_tv_title)
    TextView detailTvTitle;// 标题
    @Bind(R.id.detail_tv_desc)
    TextView detailTvDesc;// 描述
    @Bind(R.id.detail_tv_time)
    TextView detailTvTime;// 竞拍时间
    @Bind(R.id.detail_tv_gradient)
    TextView detailTvGradient;// 竞价梯度
    @Bind(R.id.detail_ll_bottom)
    LinearLayout detailLlBottom;// 底部竞价控件
    @Bind(R.id.detail_tv_owner)
    TextView detailTvOwner;// 已拍出竞品得主
    @Bind(R.id.detail_ll_owner)
    LinearLayout detailLlOwner;// 已拍出竞品得主整体控件

    private int flag = 0;// 0-首页详情 1-我发布的 2-流拍 3-已拍出 4-我参与的
    private String commodityTitle;
    private String startTime;
    private String endTime;
    private int auctionGradient;
    private String des;
    private int currentPrice;
    private int initialPrice;
    private String picPath;

    @Override
    public int getLayoutId() {
        return R.layout.activity_product_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        commodityTitle = bundle.getString("commodityTitle");
        startTime = bundle.getString("startTime");
        endTime = bundle.getString("endTime");
        auctionGradient = bundle.getInt("auctionGradient");
        des = bundle.getString("des");
        currentPrice = bundle.getInt("currentPrice");
        commodityId = bundle.getInt("commodityId");
        initialPrice = bundle.getInt("initialPrice");
        picPath = bundle.getString("picPath");
        SharedPreferences sPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sPreferences.getInt("userId", 0);

        initViews();
    }

    private void initViews() {
        customToolbar.findTextView(R.id.left_tv).setVisibility(View.GONE);
        customToolbar.findImageView(R.id.left_iv).setVisibility(View.VISIBLE);
        customToolbar.findTextView(R.id.right_tv_main).setVisibility(View.GONE);
        customToolbar.findTextView(R.id.right_tv_sub).setVisibility(View.GONE);

        customToolbar.findTextView(R.id.center_tv).setText("详情");
        customToolbar.setOnCustomToolBarClickListener(this);
        // 控制底部竞价控件
        if (flag == Constants.MY_LOT || flag == Constants.PASS_IN_LOT || flag == Constants.PASS_OUT_LOT) {
            detailLlBottom.setVisibility(View.GONE);
        } else {
            detailLlBottom.setVisibility(View.VISIBLE);
        }
        // 控制“得主”控件
        if (flag == 3) {
            detailLlOwner.setVisibility(View.VISIBLE);
        } else {
            detailLlOwner.setVisibility(View.GONE);
        }

        detailTvTitle.setText(commodityTitle);
        detailTvDesc.setText(des);
        detailTvTime.setText(startTime + "--" + endTime);
        detailTvGradient.setText(auctionGradient + "");
        if (currentPrice == 0) {
            detailTvPrice.setText("现价：" + initialPrice);
        } else {
            detailTvPrice.setText("现价：" + currentPrice);
        }
        Log.d(TAG, "initViews: " + ApiConstants.STAGING_MOBILE_TICKET_HOST + "/commodity" + picPath);
        Glide.with(mContext).load(ApiConstants.STAGING_MOBILE_TICKET_HOST + "commodity/" + picPath).into(detailIvPic);

    }

    @OnClick(R.id.detail_tv_join)
    public void onViewClicked() {
        // 点击“我要竞价”

        mRxManager.add(
                ApiRequestManager.getInstance().bidding(userId + "", commodityId + "")
                        .compose(RxSchedulers.<BiddingEntity>io_main())
                        .subscribe(new RxSubscriber<BiddingEntity>(this) {
                            @Override
                            protected void _onNext(BiddingEntity biddingEntity) {
                                ToastUtil.showShort("提交成功");

                                if (currentPrice == 0) {
                                    currentPrice = initialPrice + auctionGradient;
                                } else {
                                    currentPrice = currentPrice + auctionGradient;
                                }
                                detailTvPrice.setText("现价：" + currentPrice);
                            }

                            @Override
                            protected void _onError(String message) {

                            }
                        })
        );
    }

    @Override
    public void onTitleLeftClick(View view) {
        finish();
    }

    @Override
    public void onTitleCenterClick(View view) {

    }

    @Override
    public void onTitleRightClick(View view) {

    }

    @Override
    public void onTitleRightSubClick(View view) {

    }
}
